package com.why.amallrest.controller;

import com.why.common.utils.PageUtils;
import com.why.common.utils.Result;
import com.why.entity.ProductEntity;
import com.why.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
 *
 * @author why
 * @email 1585049082@qq.com
 * @date 2021-10-24 20:16:08
 */
@RestController
@RequestMapping("mall/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = productService.queryPage(params);

        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{productId}")
    public Result info(@PathVariable("productId") String productId){
		ProductEntity product = productService.getById(productId);

        return Result.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public Result save(@RequestBody ProductEntity product){
		productService.save(product);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ProductEntity product){
		productService.updateById(product);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public Result delete(@RequestBody String[] productIds){
		productService.removeByIds(Arrays.asList(productIds));

        return Result.ok();
    }

    @GetMapping("/recommends/{length}")

    public Result recommends(@PathVariable("length") Integer length){

        return Result.ok().put("data",productService.queryRecommendsProduct(length));
    }



    @RequestMapping("/getProductDetail/{productId}")
    public Result  getProductDetail(@PathVariable("productId") Long productId){

        if(productId==null){
            return Result.error();
        }


        Map data = this.productService.queryProductDetail(productId);

        return Result.ok().setData(data);


    }



}
